package com.cly.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cly.domain.IndexImg;
import com.cly.service.IndexImgService;
import com.cly.vo.IndexImgVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.*;
import java.util.List;


@RestController
@RequestMapping("admin/indexImg")
@Api(tags = "轮播图管理接口")
public class IndexImgController {

    @Autowired
    private IndexImgService indexImgService;

    // ------------------------ 后台接口

    /**
     * 分页查询轮播图
     * @param page
     * @param indexImg
     * @return
     */
    @GetMapping("page")
    @ApiOperation("分页查询轮播图")
    @PreAuthorize("hasAuthority('admin:indexImage:page')")
    public ResponseEntity<Page<IndexImg>> findIndexImgPage(Page<IndexImg> page, IndexImg indexImg){
        Page<IndexImg> indexImgPage = indexImgService.findIndexImgPage(page, indexImg);
        return ResponseEntity.ok(indexImgPage);
    }

    /**
     * 根据id回显轮播图
     * @return
     */
    @GetMapping("info/{id}")
    @ApiOperation("根据id回显轮播图")
    @PreAuthorize("hasAuthority('admin:indexImg:info')")
    public ResponseEntity<IndexImg> findIndexImgById(@PathVariable("id") Long id){
        IndexImg indexImg = indexImgService.findIndexImgById(id);
        return ResponseEntity.ok(indexImg);
    }

    // --------------- 前台接口

    @GetMapping("indexImgs")
    @ApiOperation("加载首页轮播图")
    public ResponseEntity<List<IndexImgVo>> loadFrontIndexImg(){
        List<IndexImgVo> indexImgVos = indexImgService.loadFrontIndexImg();
        return ResponseEntity.ok(indexImgVos);
    }

    // -------------远程调用接口
}
